﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Media;
using System.Xml.Serialization;
using System.Xml;

/// <summary>
/// List of AMediaInfo instance.
/// Use to create real playlist
/// </summary>
    [Serializable]
    public class PlayList : IPlayList
    {
        #region "Constructeurs"
        /// <summary>
        /// Constructor for serialization use PlayList(String Pname) instead
        /// </summary>
            public PlayList()
            {
            }
        /// <summary>
        /// Create a playlist with the specified name
        /// </summary>
        /// <param name="Pname"></param>
            public PlayList(String Pname)
            {
                name = Pname;
                listMedia = new List<AMediaInfo>();
            }
        #endregion

        #region "Properties"
            public String name;
            public List<AMediaInfo> listMedia = new List<AMediaInfo>();
        #endregion

        #region "Add / Delete Elem"
        /// <summary>
        /// remove an element from the playlist at the specified index
        /// </summary>
        /// <param name="index">index of the element to remove</param>
            public void removeElemFromIndex(int index)
            {
                if (listMedia != null && listMedia.Capacity > index)
                    listMedia.RemoveAt(index);
            }
        /// <summary>
        /// Add an element to the playlist
        /// </summary>
        /// <param name="med">information about the media to add</param>
            public virtual void addElem(AMediaInfo med)
            {
                if (listMedia != null)
                    listMedia.Add(med);
            }

        /// <summary>
        /// Remove an element from the playlist
        /// </summary>
            /// <param name="med">information about the media to remove</param>
            public virtual void removeElem(AMediaInfo med)
            {
                if (listMedia != null)
                    listMedia.Remove(med);
            }
            #endregion

        #region "Getters / Setters"
        /// <summary>
        /// Change the name of the playlist
        /// </summary>
        /// <param name="Pname">new name of the playlist</param>
        public virtual void setName(String Pname)
        {
            name = Pname;
        }

        public virtual List<AMediaInfo> getList()
        {
            return (listMedia);
        }

        /// <summary>
        /// Get the name of the playlist
        /// </summary>
        /// <returns></returns>
        public virtual String getName()
        {
            return (name);
        }
        #endregion

        /// <summary>
        /// Get informations about the media at the specified index
        /// </summary>
        /// <param name="id">index of the media in the playlist</param>
        /// <returns>informations about the file</returns>
        public virtual AMediaInfo getMediaById(int id)
        {
            if (listMedia != null && listMedia.Count > id && id >= 0)
                return (listMedia[id]);
            return (null);
        }

        /// <summary>
        /// Put in the playlist all the media from the specified folder
        /// </summary>
        /// <param name="path">folder path</param>
        public void getMediaInfoFromFolder(String path)
        {
            if (path != null)
            {
                String[] dir = System.IO.Directory.GetDirectories(path);
                for (int i = 0; dir != null && i < dir.Length; i++)
                    getMediaInfoFromFolder(dir[i]);
                string[] files; // pour avoir les noms des fichiers et sous-répertoires 
                files = System.IO.Directory.GetFiles(path);
                if (files != null)
                {
                    int filecount = files.Length;
                    for (int i = 0; i < filecount; i++)
                    {
                        AMediaInfo inf = MediaInfoFactory.getInfoOfMedia(files[i]);
                        if (inf != null && listMedia != null)
                            listMedia.Add(inf);
                    }
                }
            }
        }

        #region "Serialization"
        /// <summary>
        /// Serialize the playlist
        /// </summary>
        public virtual void serialize()
        {
            XmlSerializer xmls = new XmlSerializer(typeof(PlayList));
            XmlWriterSettings settings = new XmlWriterSettings();

            settings.Indent = true;
            settings.OmitXmlDeclaration = true;
            settings.NewLineOnAttributes = true;

            XmlWriter xmlx = XmlWriter.Create("serialization.xml", settings);

            if (xmlx != null && xmls != null)
            {
                xmls.Serialize(xmlx, this);
                xmlx.Close();
            }
        }

        public virtual void serialize(String path)
        {
            XmlSerializer xmls = new XmlSerializer(typeof(PlayList));
            XmlWriterSettings settings = new XmlWriterSettings();

            settings.Indent = true;
            settings.OmitXmlDeclaration = true;
            settings.NewLineOnAttributes = true;

            XmlWriter xmlx = XmlWriter.Create(path, settings);
         
            if (xmlx != null && xmls != null)
            {
                xmls.Serialize(xmlx, this);
                xmlx.Close();
            }
        }

        public static PlayList deSerialize(string path)
        {
            XmlSerializer xmls = new XmlSerializer(typeof(PlayList));
            XmlReader xmlx = XmlReader.Create(path);
            if (xmlx != null && xmls != null)
            {
                PlayList tmp = (PlayList)xmls.Deserialize(xmlx);
                xmlx.Close();
                return (tmp);
            }
            return (null);
        }

        #endregion
    }
